package org.jeecg.modules.job.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.rms.entity.RmsCompany;
import org.jeecg.modules.job.rms.entity.RmsCompanyPosition;
import org.jeecg.modules.job.rms.mapper.RmsCompanyMapper;
import org.jeecg.modules.job.rms.mapper.RmsCompanyPositionMapper;
import org.jeecg.modules.job.ums.entity.UmsAccount;
import org.jeecg.modules.job.ums.entity.UmsAccountRecords;
import org.jeecg.modules.job.ums.entity.UmsApplyLog;
import org.jeecg.modules.job.ums.entity.UmsApplyRead;
import org.jeecg.modules.job.ums.mapper.UmsApplyLogMapper;
import org.jeecg.modules.job.ums.mapper.UmsApplyReadMapper;
import org.jeecg.modules.job.ums.service.IUmsAccountRecordsService;
import org.jeecg.modules.job.ums.service.IUmsAccountService;
import org.jeecg.modules.job.ums.service.IUmsApplyLogService;
import org.jeecg.modules.job.ums.service.IUmsMemberRoleService;
import org.jeecg.modules.job.wms.entity.WmsJobExpect;
import org.jeecg.modules.job.wms.entity.WmsResume;
import org.jeecg.modules.job.wms.entity.WmsWorkExp;
import org.jeecg.modules.job.wms.mapper.WmsJobExpectMapper;
import org.jeecg.modules.job.wms.mapper.WmsResumeMapper;
import org.jeecg.modules.job.wms.mapper.WmsWorkExpMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 应聘记录
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Service
public class UmsApplyLogServiceImpl extends ServiceImpl<UmsApplyLogMapper, UmsApplyLog> implements IUmsApplyLogService {
    @Resource
    private RmsCompanyPositionMapper companyPositionMapper;
    @Resource
    private WmsResumeMapper resumeMapper;
    @Resource
    private RmsCompanyMapper companyMapper;
    @Resource
    private WmsWorkExpMapper workExpMapper;
    @Resource
    private UmsApplyReadMapper applyReadMapper;
    @Resource
    private WmsJobExpectMapper jobExpectMapper;
    @Resource
    private IUmsAccountService accountService;
    @Resource
    private IUmsAccountRecordsService accountRecordsService;
    @Resource
    private IUmsMemberRoleService memberRoleService;
    @Resource
    private ISysBaseAPI sysBaseAPI;


    @Override
    public boolean addOrUpdate(UmsApplyLog param) {
        UmsApplyLog result=this.getOne(new QueryWrapper<>(param));
        if (result!=null){
            param.setId(result.getId());
            this.updateById(param);
        }else{
            this.save(param);
        }
        return true;
    }

    @Override
    public void updateApplyStatus(UmsApplyLog param) {
        this.updateById(param);
        if (param.getStatus().equals("2")){
            //录用，查询是否分享，如果是分享的，则给分享者加入一笔佣金
            UmsApplyLog result= this.getById(param.getId());
            if (StringUtils.isNotEmpty(result.getTcode())){
                //查询佣金金额
                RmsCompanyPosition companyPosition= companyPositionMapper.selectOne(new QueryWrapper<>(new RmsCompanyPosition().setId(result.getPositionId())));
                if (companyPosition.getShareMoney().compareTo(BigDecimal.ZERO)==0){//岗位无佣金
                    return;
                }
                //根据tcode获取用户信息
                LoginUser memberResult = sysBaseAPI.getUserById(result.getTcode());
                //更新账户
                UmsAccount memberAccount=accountService.findByMemberId(memberResult.getId());
                if(memberAccount==null){
                    //创建会员账户信息
                    UmsAccount accountParam=new UmsAccount();
                    accountParam.setMemberId(memberResult.getId());
                    accountParam.setBalance(companyPosition.getShareMoney());
                    accountParam.setBalanceWithdraw(companyPosition.getShareMoney());
                    accountService.save(accountParam);
                }else{
                    memberAccount.setBalance(memberAccount.getBalance().add(companyPosition.getShareMoney()));
                    memberAccount.setBalanceWithdraw(memberAccount.getBalanceWithdraw().add(companyPosition.getShareMoney()));
                    accountService.updateById(memberAccount);
                }
                //添加收入记录
                UmsAccountRecords accountRecordsParam=new UmsAccountRecords();
                accountRecordsParam.setMemberId(memberResult.getId());
                accountRecordsParam.setMoney(companyPosition.getShareMoney());
                accountRecordsParam.setAddSign(1);
                accountRecordsParam.setDataId(param.getId());
                accountRecordsParam.setAccountType(1);
                accountRecordsParam.setContent("分享职位佣金收入");
                accountRecordsService.save(accountRecordsParam);
            }
        }
    }

    @Override
    public IPage<UmsApplyLog> findPageBySpec(Page<UmsApplyLog> page, UmsApplyLog param) {
        IPage<UmsApplyLog> pageInfo = this.baseMapper.selectPage(page, new QueryWrapper<>(param));
        if(param.getType()==0){
            page.getRecords().forEach(item -> {
                if (item.getPositionId()!=null){
                    item.setPosition(companyPositionMapper.selectOne(new QueryWrapper<>(new RmsCompanyPosition().setId(item.getPositionId()))));
                    //招聘者信息
                    LoginUser memberResult=sysBaseAPI.getUserById(item.getComMemberId());
                    item.setMemberAvatar(memberResult.getAvatar());
                    item.setMemberName(memberResult.getRealname());
                    item.setMemberPostName(memberRoleService.getMemberRole(memberResult.getId()).getPostName());
                    //企业信息
                    RmsCompany company=companyMapper.selectById(item.getCompanyId());
                    item.setCompanyName(company.getName());
                    item.setCompanyStaffSize(company.getStaffSize());
                    item.setCompanyNature(company.getCompanyNature());
                }
            });
        }else{
            page.getRecords().forEach(item -> {
                item.setResume(resumeMapper.selectOne(new QueryWrapper<>(new WmsResume().setMemberId(item.getMemberId()))));
                //求职者信息
                LoginUser memberResult=sysBaseAPI.getUserById(item.getMemberId());
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getRealname());
                item.setMemberPostName(memberRoleService.getMemberRole(memberResult.getId()).getPostName());
                item.setMemberSex(memberResult.getSex()+"");
                //工作经历
                List<WmsWorkExp> list=workExpMapper.selectList(new QueryWrapper<>(new WmsWorkExp().setMemberId(item.getMemberId())));
                if (list.size()>0){
                    item.setCompanyName(list.get(0).getCompanyName());
                    item.setCompanyPostName(list.get(0).getPostName());
                    item.setWorkSkill(list.get(0).getSkill());
                }
                //期望薪资
                List<WmsJobExpect> expectResults =jobExpectMapper.selectList(new QueryWrapper<>(new WmsJobExpect().setId(item.getMemberId())));
                if (expectResults.size()>0){
                    item.setExpectSalary(expectResults.get(0).getExpectSalary());
                }
                //应聘岗位名称
                RmsCompanyPosition companyPositionResult =companyPositionMapper.selectOne(new QueryWrapper<>(new RmsCompanyPosition().setId(item.getPositionId())));
                if (companyPositionResult!=null){
                    item.setPostName(companyPositionResult.getPostName());
                }
                //是否已读
                UmsApplyRead readResult =applyReadMapper.selectOne(new QueryWrapper<>(new UmsApplyRead().setApplyId(item.getId())));
                if (readResult!=null){
                    item.setIfRead(true);
                }
            });
        }
        return pageInfo;
    }
}
